package org.leon.controller;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.leon.bean.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/20.
 */
@Controller
@RequestMapping("/json")
public class BookController {
    private static final Log logger = LogFactory.getLog(BookController.class);
    @RequestMapping(value="/testRequestBody",consumes = "application/json")
    public void setJson(@RequestBody Book book, HttpServletResponse response) throws IOException {
        logger.info(JSONObject.toJSONString(book));
        book.setAuthor("胡国栋");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSONObject.toJSONString(book));
    }

    @RequestMapping(value = "/testResponseBody")
    @ResponseBody
    public Object getJson(){
        List<Book> books = new ArrayList<>();
        books.add(new Book(1,"TT","tw"));
        books.add(new Book(2,"TA","中国"));
        return books;
    }
}
